home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 December / CHIPNET Aralık 1997.iso / linux / redhat / misc / src / install / genhdl~1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-11  |  3.7 KB  |  158 lines

  1. #include <alloca.h>
  2. #include <ctype.h>
  3. #include <dirent.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <rpmlib.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <unistd.h>
  12.  
  13. #define FILENAME_TAG 1000000
  14.  
  15. int tags[] =  { RPMTAG_NAME, RPMTAG_VERSION, RPMTAG_RELEASE, RPMTAG_GROUP,
  16.         RPMTAG_REQUIREFLAGS, RPMTAG_REQUIRENAME, RPMTAG_REQUIREVERSION,
  17.         RPMTAG_DESCRIPTION, RPMTAG_SUMMARY, RPMTAG_PROVIDES,
  18.         RPMTAG_SIZE };
  19. int numTags = sizeof(tags) / sizeof(int);
  20.  
  21. int ugdbTags[] =  { RPMTAG_NAME, RPMTAG_VERSION, RPMTAG_RELEASE, RPMTAG_SERIAL,
  22.         RPMTAG_FILENAMES, RPMTAG_PROVIDES, RPMTAG_REQUIREFLAGS,
  23.         RPMTAG_REQUIRENAME, RPMTAG_REQUIREVERSION };
  24.  
  25. int numUgdbTags = sizeof(ugdbTags) / sizeof(int);
  26.  
  27. int main(int argc, char ** argv) {
  28.     char buf[300];
  29.     DIR * dir;
  30.     int outfd, ugdboutfd;
  31.     struct dirent * ent;
  32.     int fd, rc, isSource;
  33.     Header h, newh;
  34.     int count, type;
  35.     int i;
  36.     void * ptr;
  37.  
  38.     if (argc != 2) {
  39.     fprintf(stderr, "usage: genhdlist <dir>\n");
  40.     exit(1);
  41.     }
  42.  
  43.     strcpy(buf, argv[1]);
  44.     strcat(buf, "/RedHat/RPMS");
  45.  
  46.     dir = opendir(buf);
  47.     if (!dir) {
  48.     fprintf(stderr,"error opening directory %s: %s\n", buf,
  49.         strerror(errno));
  50.     return 1;
  51.     }
  52.     chdir(buf);
  53.  
  54.     strcpy(buf, argv[1]);
  55.     strcat(buf, "/RedHat/base/hdlist");
  56.     
  57.     outfd = open(buf, O_WRONLY | O_TRUNC | O_CREAT, 0644);
  58.     if (outfd < 0) {
  59.     fprintf(stderr,"error creating file %s: %s\n", buf, strerror(errno));
  60.     return 1;
  61.     }
  62.  
  63.     strcpy(buf, argv[1]);
  64.     strcat(buf, "/RedHat/base/uglist");
  65.     
  66.     ugdboutfd = open(buf, O_WRONLY | O_TRUNC | O_CREAT, 0644);
  67.     if (ugdboutfd < 0) {
  68.     fprintf(stderr,"error creating file %s: %s\n", buf, strerror(errno));
  69.     return 1;
  70.     }
  71.  
  72.     errno = 0;
  73.     ent = readdir(dir);
  74.     if (errno) {
  75.     perror("readdir");
  76.     return 1;
  77.     }
  78.  
  79.     while (ent) {
  80.     if (!(ent->d_name[0] == '.' && (ent->d_name[1] == '\0' || 
  81.         ((ent->d_name[1] == '.') && (ent->d_name[2] == '\0'))))) {
  82.         fd = open(ent->d_name, O_RDONLY);
  83.  
  84.         if (fd < 0) {
  85.         perror("open");
  86.         exit(1);
  87.         }
  88.  
  89.         rc = rpmReadPackageHeader(fd, &h, &isSource, NULL, NULL);
  90.  
  91.         close(fd);
  92.         if (rc) {
  93.         fprintf(stderr, "failed to rpmReadPackageHeader %s\n", ent->d_name);
  94.         exit(1);
  95.         }
  96.  
  97.         /* Regular bit */
  98.         newh = headerNew();
  99.         for (i = 0; i < numTags; i++) {
  100.         if (!headerGetEntry(h, tags[i], &type, &ptr, &count)) {
  101.             switch (tags[i]) {
  102.               case RPMTAG_REQUIREFLAGS:
  103.               case RPMTAG_REQUIRENAME:
  104.               case RPMTAG_REQUIREVERSION:
  105.               case RPMTAG_SUMMARY:
  106.               case RPMTAG_PROVIDES:
  107.             break;
  108.               default:
  109.             fprintf(stderr, "tag %d not found in file %s\n", 
  110.                 tags[i], ent->d_name);
  111.             exit(1);
  112.             }
  113.         } else 
  114.             headerAddEntry(newh, tags[i], type, ptr, count);
  115.         }
  116.         headerAddEntry(newh, FILENAME_TAG, RPM_STRING_TYPE, ent->d_name, 1);
  117.         headerWrite(outfd, newh, HEADER_MAGIC_YES);
  118.         headerFree(newh);
  119.  
  120.         /* Upgrade bit */
  121.         newh = headerNew();
  122.         for (i = 0; i < numUgdbTags; i++) {
  123.         if (!headerGetEntry(h, ugdbTags[i], &type, &ptr, &count)) {
  124.             switch (ugdbTags[i]) {
  125.               case RPMTAG_SERIAL:
  126.               case RPMTAG_PROVIDES:
  127.               case RPMTAG_REQUIREFLAGS:
  128.               case RPMTAG_REQUIRENAME:
  129.               case RPMTAG_REQUIREVERSION:
  130.             break;
  131.               default:
  132.             fprintf(stderr, "tag %d not found in file %s\n", 
  133.                 ugdbTags[i], ent->d_name);
  134.             exit(1);
  135.             }
  136.         } else
  137.             headerAddEntry(newh, ugdbTags[i], type, ptr, count);
  138.         }
  139.         headerWrite(ugdboutfd, newh, HEADER_MAGIC_YES);
  140.         headerFree(newh);
  141.         
  142.         headerFree(h);
  143.     }
  144.  
  145.     errno = 0;
  146.     ent = readdir(dir);
  147.     if (errno) {
  148.         perror("readdir");
  149.         return 1;
  150.     }
  151.     } 
  152.  
  153.     closedir(dir);
  154.     close(outfd);
  155.  
  156.     return 0;
  157. }
  158.